<template>
  <div class="container">
    <div class="container-box">
      <div class="img-box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <div class="login-box">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username">
            <el-input 
              prefix-icon="el-icon-user" 
              v-model="loginForm.username">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input 
              prefix-icon="el-icon-goods" 
              type="password" 
              show-password
              v-model="loginForm.password">
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="resetBtn">重置</el-button>
            <el-button type="primary" @click="loginBtn">登录</el-button>           
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm:{
        username:'admin',
        password:'123456'
      },
      loginRules:{
        username:[
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    resetBtn(){
      this.$refs.loginForm.resetFields()
    },
    loginBtn(){
      this.$refs.loginForm.validate(async value => {
        if(!value) return 
        const {data:res} = await this.$http.post('login',this.loginForm)
        if(res.meta.status !== 200) return this.$message.error('登陆失败')
        window.sessionStorage.setItem('token',res.data.token)
        this.$router.push('/home')
        this.$message.success('登陆成功')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  background-color: rgb(43, 77, 110);
  height: 100%;
  position: relative;

  .container-box {
    background-color: #fff;
    width: 450px;
    height: 350px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%) !important;
    border-radius: 10px;

    .img-box{
      width: 150px;
      height: 150px;
      border-radius: 50%;
      padding: 10px;
      border: 1px solid #bbb;
      box-shadow: 0 0 5px #bbb;
      background-color: #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%,-35%);
      
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #ddd;
      }
    }

    .login-box{
      margin-top: 150px;
      padding: 0 20px;
    }
  }
}

.el-form-item{
  width: 100%;

  .el-input{
    width: 100%;
  }

  .el-button{
    float: right;
    margin-left: 10px;
  }   
}
</style>